unit uDZConfig;

interface

uses
  SysUtils, Classes, ShlObj, TypInfo, uDZSerializer, uDZPersistent;

type
  TDzDBAccessEngine = (daeMSADO);

  TDzDBConnectionParam = class(TDzPersistent)
  private
    fUserID: string;
    fPassword: string;
  protected
    procedure WriteProperties(const writer: TDzClassWriter;
      options: TDzArchiveOptions); override;
  public
    class function GetEngine: TDzDBAccessEngine; virtual; abstract;
  published
    property UserID: string read fUserID write fUserID;
    property Password: string read fPassword write fPassword;
  end;

  TDzADOConnectionParam = class(TDzDBConnectionParam)
  private
    fConnectionTimeout: Integer;
    fCommandTimeout: Integer;
  protected
    function GetConnString: string; virtual; abstract;
  public
    class function GetEngine: TDzDBAccessEngine; override;
    constructor Create; override;
    property ConnString: string read GetConnString;
  published
    property ConnectionTimeout: Integer read
      fConnectionTimeout write fConnectionTimeout;
    property CommandTimeout: Integer read fCommandTimeout write fCommandTimeout;
  end;

  TDzMSAccessADOCP = class(TDzADOConnectionParam)
  private
    fFileName: string;
  protected
    function GetConnString: string; override;
  published
    property FileName: string read fFileName write fFileName;
  end;

  TDzMSSQLADOCP = class(TDzADOConnectionParam)
  private
    fServer: string;
    fPort: Integer;
  protected
    function GetConnString: string; override;
  published
    property Server: string read fServer write fServer;
    property Port: Integer read fPort write fPort;
  end;

function GetAppDataPath: string;

implementation

function GetAppDataPath: string;
const
  MAX_PATH = 260;
var
  Path: array [0..MAX_PATH] of Char;
begin
  SHGetSpecialFolderPath(0, Path, CSIDL_APPDATA, True);
  SetString(Result, Path, StrLen(Path));
end;

{ TDzADOConnectionParam }

constructor TDzADOConnectionParam.Create;
begin
  inherited;
  fConnectionTimeout := 3;
  fCommandTimeout := 1;
  fCommandTimeout := 1;
end;

class function TDzADOConnectionParam.GetEngine: TDzDBAccessEngine;
begin
  Result := daeMSADO;
end;

{ TDzDBConnectionParam }

procedure TDzDBConnectionParam.WriteProperties(
  const writer: TDzClassWriter; options: TDzArchiveOptions);
begin
  inherited;
  writer.WriteString('Engine', GetEnumName(
    TypeInfo(TDzDBAccessEngine), Ord(GetEngine)));
end;

{ TDzMSSQLADOCP }

function TDzMSSQLADOCP.GetConnString: string;
begin
  if UserID = '' then
    Result := Format('Provider=SQLOLEDB.1;Network Address="%s,%d"',
      [Server, Port])
  else
    Result := Format('Provider=SQLOLEDB.1;Network Address="%s,%d";' +
      'UID=%s;PWD=%s;', [Server, Port, UserID, Password]);
end;

{ TDzMSAccessADOCP }

function TDzMSAccessADOCP.GetConnString: string;
begin
  if UserID = '' then
    Result := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source="%s"',
      [FileName])
  else
    Result := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source="%s";' +
      'UID=%s;PWD=%s;', [FileName, UserID, Password]);
end;

end.
